﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using QuanLyDuocPham.Controllers.QuanLyHeThong.DanhMuc;
using QuanLyDuocPham.Controllers.QuanLyHeThong.NhapXuat;


namespace QuanLyDuocPham.Controllers.UC.QuanLyHeThong.NhapXuat
{
    /// <summary>
    /// Interaction logic for UC_NHAP_CHITIET.xaml
    /// </summary>
    public partial class UC_NHAP_CHITIET : UserControl
    {
        public UC_NHAP_CHITIET()
        {
            InitializeComponent();
        }
        public delegate void added();
        public added done;

        string ma_nhap;


        public string _ma_nhap
        {
            set
            {
                ma_nhap = value;
            }
        }
        
        Boolean is_edit;


        public Boolean _is_edit
        {
            set
            {
                is_edit = value;
            }
        }
        DUOCPHAM_Control dpctrl = new DUOCPHAM_Control();
        GIA_Control giactrl = new GIA_Control();
        NHAP_CHITIET_Control nhctctrl = new NHAP_CHITIET_Control();
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            btnNHAPCT_Save.IsEnabled = false;
            dpctrl.loadComboBox(cmbListDP);
            if (is_edit == true)
            {
                nhctctrl.loadDataTemp2_Edit(ma_nhap,lvNHAPCT_List);
            }
        }

        private void cmbListDP_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            txtGia_Ban.Text = giactrl.loadGiaSiDP(cmbListDP);
        }

        private void btnNHAPCT_Add_Click(object sender, RoutedEventArgs e)
        {
            nhctctrl.XuLyBangTam("add", ma_nhap, lvNHAPCT_List, cmbListDP, dtpNSX, dtpHSD, txtSoLuong, txtGia_Ban, txtChiet_Khau, txtGhi_Chu);
            //nhctctrl.addData(ma_nhap, lvNHAPCT_List, cmbListDP, txtSo_Lo, dtpHSD, txtSoLuong, txtGia_Ban, txtChiet_Khau, txtGhi_Chu);
            //nhctctrl.loadDataTemp_Add(lvNHAPCT_List);
            nhctctrl.addandloadDataTemp2();
            clearNHAPCT();
            nhctctrl.TongTien(lbTongTien);
        }
        private void clearNHAPCT()
        {
            cmbListDP.SelectedItem=null;
            txtSo_Lo.Clear();
            dtpHSD.SelectedDate = DateTime.Now;
            txtSoLuong.Clear();
            txtGia_Ban.Clear();
            txtChiet_Khau.Clear();
            txtGhi_Chu.Document.Blocks.Clear();
 
        }

        private void btnNHAPCT_Delete_Click(object sender, RoutedEventArgs e)
        {
            //var sl = (TEMP2_Get_ListResult)lvNHAPCT_List.SelectedItem;
            //nhctctrl.deleteData(sl.MA_DUOCPHAM, ma_nhap);
            nhctctrl.XuLyBangTam("delete", ma_nhap, lvNHAPCT_List, cmbListDP, dtpNSX, dtpHSD, txtSoLuong, txtGia_Ban, txtChiet_Khau, txtGhi_Chu);
            nhctctrl.addandloadDataTemp2();
            nhctctrl.TongTien(lbTongTien);


            //nhctctrl.loadDataTemp_Add(lvNHAPCT_List);

        }

        private void btnNHAPCT_Save_Click(object sender, RoutedEventArgs e)
        {
            //nhctctrl.updateData(lvNHAPCT_List, ma_nhap, cmbListDP, txtSo_Lo, dtpHSD, txtSoLuong, txtGia_Ban, txtChiet_Khau, txtGhi_Chu);
            nhctctrl.XuLyBangTam("update", ma_nhap, lvNHAPCT_List, cmbListDP, dtpNSX, dtpHSD, txtSoLuong, txtGia_Ban, txtChiet_Khau, txtGhi_Chu);
            nhctctrl.addandloadDataTemp2();
            
            btnNHAPCT_Save.IsEnabled = false;
            btnNHAPCT_Add.IsEnabled = true;
            clearNHAPCT();
            lvNHAPCT_List.SelectedItem = null;
            nhctctrl.TongTien(lbTongTien);

        }

        private void lvNHAPCT_List_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            btnNHAPCT_Save.IsEnabled = true;
            btnNHAPCT_Add.IsEnabled = false;
            nhctctrl.TamToView( ma_nhap, lvNHAPCT_List, cmbListDP, dtpNSX, dtpHSD, txtSoLuong, txtGia_Ban, txtChiet_Khau, txtGhi_Chu);
        }

        private void btnClear_NHAPCT_Click(object sender, RoutedEventArgs e)
        {
            clearNHAPCT();
        }

        private void btnSaveAll_Click(object sender, RoutedEventArgs e)
        {
            
            nhctctrl.deleteAllNhapCT(ma_nhap);
            nhctctrl.TamToNHAPCT();
        }

        private void btnUndo_Click(object sender, RoutedEventArgs e)
        {
            nhctctrl.NHAPCTToTam(ma_nhap);
        }



    }
}
